$(function () {
    loadNews();
    // getNewsData(1, 0);
});

function loadNews() {
    let news_type = sessionStorage.getItem("news_type");
    sessionStorage.removeItem("news_type");//获取之后马上清除，以不影响其他页面的操作
    getNewsData(1,news_type);
}

function getNewsData(page, news_type) {
    var newsType = 0;
    var pageNum = 1;
    if (page == null || page == '')
        page = pageNum;
    if (news_type == null || news_type == '')
        news_type = newsType;
    let data = myAjax('/news', 'post', {"news_type": news_type, "page": page}, 'json');
    sessionStorage.setItem("newsData", JSON.stringify(data));
    console.log(data);
    $(".news_type").html("");
    $.each(data.newsTypes, function (i, newstype) {
        if ((i + 1) == news_type)
            $(".news_type").append("<div class='currentNewsType' onclick='getNewsData(1," + newstype.id + ")'>" + newstype.name + "</div>");
        else
            $(".news_type").append("<div onclick='getNewsData(1," + newstype.id + ")'>" + newstype.name + "</div>");
    });
    let pageCount = Math.ceil(data.newsCount / 10);
    $(".news_list").html("");
    $("#page").html("");
    if (page <= pageCount) {
        $.each(data.news, function (i, news) {
            $(".news_list").append("<div><a class='header'><div class='type' onclick='getNewsData(1," + news.newsType.id + ")'>[" + news.newsType.name + "]</div><div class='title' onclick='redirectNewsDetails(" + i + ")'>" + news.title + "</div></a><div class='date'>" + news.create_time + "</div></div>");
        });
        $("#page").html("<div id='last' onclick='getNewsData(" + (parseInt(page) - 1) + "," + news_type + ")'>&lt;</div>" +
            "<div id='right' onclick='getNewsData(" + (parseInt(page) + 1) + "," + news_type + ")'>&gt;</div>");
        if (pageCount <= 5) {
            for (let i = pageCount; i >= 1; i--) {
                if (i == page)
                    $("#last").after("<div class='currentPage' onclick='getNewsData(" + i + "," + news_type + ")'>" + i + "</div>");
                else
                    $("#last").after("<div onclick='getNewsData(" + i + "," + news_type + ")'>" + i + "</div>");
            }
        } else {
            if (pageCount - page >= 5) {
                $("#last").after("<div>...</div><div onclick='getNewsData(" + pageCount + "," + news_type + ")'>" + pageCount + "</div>");
                for (let i = page + 4; i >= page; i--) {
                    if (i == page)
                        $("#last").after("<div class='currentPage' onclick='getNewsData(" + i + "," + news_type + ")'>" + i + "</div>");
                    else
                        $("#last").after("<div onclick='getNewsData(" + i + "," + news_type + ")'>" + i + "</div>");
                }
            } else {
                for (let i = pageCount; i >= pageCount - 4; i--) {
                    if (i == page)
                        $("#last").after("<div class='currentPage' onclick='getNewsData(" + i + "," + news_type + ")'>" + i + "</div>");
                    else
                        $("#last").after("<div onclick='getNewsData(" + i + "," + news_type + ")'>" + i + "</div>");
                }
            }
        }
    }

};

function redirectNewsDetails(i) {
    sessionStorage.setItem("newsi", i);
    window.location.href = "/html/front/newsdetails.html";
}